//
//  XGQMController.m
//  YanTaiBang
//
//  Created by broad zou on 12-9-6.
//  Copyright (c) 2012年 UEXSS. All rights reserved.
//

#import "UEXInputController.h"
#import <QuartzCore/QuartzCore.h>
#import "NetUtility.h"
#import "NSString+Alert.h"
#import "URLConfig.h"
//#import "AccountController.h"
//#import "LoginController.h"
#import "MD5Digest.h"
#import "URLConfig.h"
#define kViewTagSmileImage 100

@implementation UEXInputController

@synthesize delegate = _delegate;
@synthesize textValue = _textValue;
@synthesize btnTitleLeft = _btnTitleLeft;
@synthesize btnTitleRight = _btnTitleRight;
@synthesize imgLeft = _imgLeft;
@synthesize imgRight = _imgRight;
@synthesize dataID = _dataID;
@synthesize dataType = _dataType;
@synthesize inputType = _inputType;
@synthesize replyID = _replyID;

#pragma mark -
#pragma mark lifeCircle
#pragma mark -
- (void)dealloc {
	//for view
	[_nullView release];
	[_indicatorView release];
	[_textView release];
	[_label release];
	[_textValue release];
	[_btnTitleLeft release];
	[_btnTitleRight release];
	[_imgLeft release];
	[_imgRight release];
	[_dataID release];
	[_dataType release];
    [_vSmile release];
	[_replyID release];
	//for data
	
    [super dealloc];
}

- (void)viewDidUnload {
	[super viewDidUnload];
	//for view
	[_nullView release];
	_nullView = nil;
	[_indicatorView release];
	_indicatorView = nil;
	[_textView release];
	_textView = nil;
	[_label release];
	_label = nil;
    [_vSmile release];
    _vSmile = nil;
	self.textValue = nil;
	self.btnTitleLeft = nil;
	self.btnTitleRight = nil;
	self.imgLeft = nil;
	self.imgRight = nil;
	self.dataID = nil;
	self.dataType = nil;
	self.replyID = nil;
	//for data
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
	[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
    float version = [[[UIDevice currentDevice] systemVersion] floatValue];
    if (version >= 5.0) {
        [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillChangeFrameNotification object:nil];
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
	self.view.backgroundColor = [AppUtility getViewBGColor];
	[self initData];
	[self initNavBtns];
	_viewInitDone = false;
	[self initViews];
	[self showIndicator:NO];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
	[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
    float version = [[[UIDevice currentDevice] systemVersion] floatValue];
    if (version >= 5.0) {
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillChangeFrameNotification object:nil];
    }
}

- (void)viewWillAppear:(BOOL)animated {
	[super viewWillAppear:animated];
}

- (void)viewWillDisappear:(BOOL)animated {
	[super viewWillDisappear:animated];
}

- (void)viewDidAppear:(BOOL)animated {
	[super viewDidAppear:animated];
}

- (void)viewDidDisappear:(BOOL)animated {
	[super viewDidDisappear:animated];
}
// for views
#pragma mark -
#pragma mark for views
#pragma mark -
- (void)initNavBtns {
	if (self.btnTitleLeft) {
		self.navigationItem.leftBarButtonItem = [AppUtility getNavBtnItemLeftWith:@selector(leftNavBtnClicked) andTitle:self.btnTitleLeft andTarget:self];
	} else {
		self.navigationItem.leftBarButtonItem = [AppUtility getNavBackBtnItemWith:@selector(leftNavBtnClicked) andTitle:nil andTarget:self];
	}
	self.navigationItem.rightBarButtonItem = [AppUtility getNavBtnItemRightWith:@selector(rightNavBtnClicked) andTitle:self.btnTitleRight andTarget:self];
}

- (void)initViews {	
	if (_viewInitDone) {
		return;
	}
	_viewInitDone = YES;
	//textView
	{
		_textView = [[UITextView alloc] initWithFrame:CGRectMake(5, 5, 310, 416-_keyboardHeight-10)];
		_textView.delegate = self;
		_textView.layer.cornerRadius = 5;
		_textView.layer.borderColor = [UIColor colorWithWhite:0.8 alpha:1.0].CGColor;
		_textView.layer.borderWidth = 2;
		_textView.font = [UIFont systemFontOfSize:16]; 
		_textView.keyboardType = UIKeyboardTypeDefault;
		_textView.text = self.textValue;
		[self.view addSubview:_textView];
		[_textView becomeFirstResponder];
	}
	//label
	{
		_label = [[UILabel alloc] initWithFrame:CGRectMake(15, 416-_keyboardHeight-5-20, 95, 16)];
		_label.backgroundColor = [UIColor clearColor];
        if (self.textValue && self.textValue.length != 0) {
            _label.text = [NSString stringWithFormat:@"%d/140", self.textValue.length];
        } else {
            _label.text = @"0/140";
        }
		_label.textColor = [UIColor lightGrayColor];
		_label.font = [UIFont systemFontOfSize:16];
		_label.textAlignment = UITextAlignmentLeft;
		[self.view addSubview:_label];
	}
}

- (void)showNullView:(BOOL)show_ {
	if (!_nullView) {
		_nullView = [[NoDataView alloc] initWithFrame:CGRectMake(0, 0, 320, 416) text:@"暂无数据"];
		[self.view addSubview:_nullView];
	}
	_nullView.hidden = !show_;
	[self.view bringSubviewToFront:_nullView];
}

- (void)showIndicator:(BOOL)show_ {
	if (show_) {
		if (!_indicatorView) {
			_indicatorView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
			_indicatorView.frame = CGRectMake(150, 190, 20, 20);
			[_indicatorView startAnimating];
		}
		[self.view addSubview:_indicatorView];
	}
	else {
		[_indicatorView removeFromSuperview];
	}	
}

- (void)haveNoData {
	[self showNullView:YES];
	[self showIndicator:NO];
}

#pragma mark -
#pragma mark for event
#pragma mark -
- (void)leftNavBtnClicked {
	[self.navigationController popViewControllerAnimated:YES];
}
/*
- (void)rightNavBtnClicked {
    if (!_textView.text || [_textView.text length]==0) {
        [@"留言不能为空" showAlert];
        return;
    }
    NSMutableDictionary *paramDict =[[[NSMutableDictionary alloc] init] autorelease];
    [paramDict setObject:[[AccountController getAccountInfo] objectForKey:kPAccountName] forKey:@"username"];
    NSString *md5Key = [MD5Digest md5:[[AccountController getAccountInfo] objectForKey:kPPassword]];
    [paramDict setObject:md5Key forKey:@"password"];
    [paramDict setObject:@"0" forKey:@"pid"];
    [paramDict setObject:_textView.text forKey:@"content"];
    [paramDict setObject:[[AccountController getAccountInfo] objectForKey:kPUid] forKey:@"uid"];
    [paramDict setObject:self.dataID forKey:@"gid"];
    [paramDict setObject:kAppKey forKey:@"AppKey"];
    [paramDict setObject:kAppSecret forKey:@"AppSecrt"];
	switch (self.inputType) {
		case KUEXInputTypeExchange:
		{
			NSDictionary *dict =  dict = [NetUtility getDataWithURLStr:kURLInsertGoodComment andPostDict:paramDict andView:self.navigationController.view];;
			UEXNSLogMethod
			UEXNSLog(@"dict is %@", dict);
			if ([dict isKindOfClass:[NSDictionary class]]) {
				if ([[dict objectForKey:@"result"] intValue] == 1) {
					[self.navigationController popViewControllerAnimated:YES];
				if (self.delegate!=nil && [(NSObject*)self.delegate respondsToSelector:@selector(modifySuccess:)]) {
						[self.delegate modifySuccess:_textView.text];
					}
				} else {
					NSString *error = [dict objectForKey:@"message"];
					if ([error isKindOfClass:[NSString class]]) {
						[error showAlert];
					}
				}
			}
			break;
		}
		case kUEXInputTypeSale:
		{
			NSDictionary *dict =  dict = [NetUtility getDataWithURLStr:kURLInsertSaleComment andPostDict:paramDict andView:self.navigationController.view];;
			UEXNSLogMethod
			UEXNSLog(@"dict is %@", dict);
			if ([dict isKindOfClass:[NSDictionary class]]) {
				if ([[dict objectForKey:@"result"] intValue] == 1) {
					[self.navigationController popViewControllerAnimated:YES];
                    if (self.delegate!=nil && [(NSObject*)self.delegate respondsToSelector:@selector(modifySuccess:)]) {
						[self.delegate modifySuccess:_textView.text];
					}
				} else {
					NSString *error = [dict objectForKey:@"message"];
					if ([error isKindOfClass:[NSString class]]) {
						[error showAlert];
					}
				}
			}
			break;
		}
		default:
		{
			[self.navigationController popViewControllerAnimated:YES];
			if (self.delegate!=nil && [(NSObject*)self.delegate respondsToSelector:@selector(modifySuccess:)]) {
				[self.delegate modifySuccess:_textView.text];
			}
			break;
		}
	}
}
*/
- (void)titleNavBtnClicked {
//	UEXNSLogMethod
}

- (void)btnSmileClicked {
	[_textView resignFirstResponder];
    _vSmile.hidden = NO;
}

- (void)btnInSmileViewClicked:(UIButton*)btn_ {
    int smileIndex = btn_.tag - kViewTagSmileImage;
    NSMutableString *textValue = (NSMutableString*)_textView.text;
    [textValue appendString:[NSString stringWithFormat:@"[%d]", smileIndex]];
    _textView.text = textValue;
}

#pragma mark -
#pragma mark for data
#pragma mark -
- (void)initData {
    _keyboardHeight = 216;
    float version = [[[UIDevice currentDevice] systemVersion] floatValue];
    if (version >= 5.0) {
        _keyboardHeight = 252;
    }
}

- (void)cleanData {
}

#pragma mark -
#pragma mark for delegate
#pragma mark -
#pragma mark UITextViewDelegate
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
    if (range.location >= 140) {  
        return  NO;  
    } else {  
        return YES;  
    }  
}  

- (void)textViewDidChange:(UITextView *)textView {  
	int num = [textView.text length];
	_label.text = [NSString stringWithFormat:@"%d/140",num]; 
}  

#pragma mark -
#pragma mark for notifications
#pragma mark -
- (void)keyboardWillShow:(NSNotification *)notification {
    /*
     Reduce the size of the text view so that it's not obscured by the keyboard.
     Animate the resize so that it's in sync with the appearance of the keyboard.
     */
    
    //NSDictionary *userInfo = [notification userInfo];
    
    // Get the origin of the keyboard when it's displayed.
    //NSValue* aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
    
    // Get the top of the keyboard as the y coordinate of its origin in self's view's coordinate system. The bottom of the text view's frame should align with the top of the keyboard's final position.
    //CGRect keyboardRect = [aValue CGRectValue];
    
    // Get the duration of the animation.
    //NSValue *animationDurationValue = [userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey];
    //NSTimeInterval animationDuration;
    //[animationDurationValue getValue:&animationDuration];
}

- (void)keyboardWillHide:(NSNotification *)notification {
}

@end
